unit Unit7; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Buttons, sqldb, Unit1;

type

  { TForm7 }

  TForm7 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    SQLQuery1: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow;
  private
    { private declarations }
  public
    codigo : String;
  end; 

var
  Form7: TForm7; 

implementation

{ TForm7 }

procedure TForm7.FormShow;
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
  
  if codigo <> '' then
  begin
    SQLQuery1.DataBase := Form1.PQConnection1;
    SQLQuery1.Transaction := Form1.SQLTransaction1;
    SQLQuery1.SQL.Text := 'select * from fabricante where fab_cdg = ' + codigo;
    
    SQLQuery1.Open;
    
    while not SQLQuery1.EOF do
    begin
      Edit2.Text := SQLQuery1.Fields[1].AsString;
      Edit3.Text := SQLQuery1.Fields[2].AsString;
      SQLQuery1.Next;
    end;
    
    Edit1.Text := codigo;

    SQLQuery1.Close;
    
    Edit2.SetFocus;
  end
  else
    Close;
end;

procedure TForm7.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm7.Button1Click(Sender: TObject);
var
  QueryString : String;
  
begin
  if Edit2.Text <> '' then
  begin
    QueryString := 'update fabricante set fab_nombre = ''' + Edit2.text + ''', fab_descripcion = '''
      + Edit3.Text + ''' where fab_cdg = ' + Edit1.Text;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');
    
    ShowMessage('MODIFICACIÓN EXITOSA');
    Close;
  end
  else
    ShowMessage('ERROR EN EL INGRESO DE DATOS');
end;

procedure TForm7.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
end;

initialization
  {$I unit7.lrs}

end.

